package cn.exrick.xboot.modules.base.controller.common;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.service.WeChatPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(tags = "微信支付接口")
@RequestMapping("/xboot/wechatPay")
public class WeChatPayController {

    @Autowired
    private WeChatPayService weChatPayService;

    /**
     * 统一下单支付结果通知回调
     *
     * @param request
     * @return
     */
    @PostMapping("/notify/order")
    @ApiOperation("支付通知")
    public ResponseEntity<String> parseOrderNotifyResult(@RequestBody String callback, HttpServletRequest request) {
        log.info("支付通知,内容{}",callback);
        String result = weChatPayService.parseOrderNotifyV3Result(callback, request);
        return ResponseEntity.ok(result);
    }

    /**
     * 退款结果通知回调
     *
     * @param request
     * @return
     */
    @PostMapping("/notify/refund")
    @ApiOperation("退款通知")
    public ResponseEntity<String> parseRefundNotifyResult(@RequestBody String callback, HttpServletRequest request) {
        log.info("退款通知,内容{}",callback);
        String result = weChatPayService.parseRefundNotifyV3Result(callback, request);
        return ResponseEntity.ok(result);
    }

    /**
     * 取消支付
     * @param dealRecordId
     * @return
     */
    @PostMapping("/wechat-pay/{dealRecordId}/action-cancel")
    @ApiOperation("取消支付")
    public ResponseEntity<String> closeOrder(@PathVariable Long dealRecordId) {
        weChatPayService.closeOrderV3(dealRecordId);
        return ResponseEntity.ok("");
    }

}
